package com.piece.core.web.exception;

import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.exception.NotFoundException;
import com.piece.core.framework.exception.PermissionException;
import com.piece.core.framework.util.basic.I18nUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import javax.sql.rowset.serial.SerialException;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统异常统一处理
 */
@Slf4j
//@ControllerAdvice
public class WebExceptionHandle {

    /**
     * 403 - 非法参数
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public ModelAndView illegalArgumentException(IllegalArgumentException e) {
        log.error("非法参数", e);
        Map<String, Object> model = new HashMap<>();
        model.put("errTitle", I18nUtil.message(ExceptionConstants.ILLEGAL));
        model.put("errInfo", e.getMessage());
        return new ModelAndView("/error/403", model);
    }

    /**
     * 403 - Bad Request
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ModelAndView handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.error("参数解析失败", e);
        Map<String, Object> model = new HashMap<>();
        model.put("errTitle", I18nUtil.message(ExceptionConstants.ARGUMENT));
        model.put("errInfo", e.getMessage());
        return new ModelAndView("/error/403", model);
    }

    /**
     * 500 - Method Not Allowed
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ModelAndView handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        log.error("不支持当前请求方法", e);
        Map<String, Object> model = new HashMap<>();
        model.put("errTitle", I18nUtil.message(ExceptionConstants.METHOD, e.getMethod()));
        model.put("errInfo", "");
        return new ModelAndView("/error/500", model);
    }

    /**
     * 500 - Unsupported Media Type
     */
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public ModelAndView handleHttpMediaTypeNotSupportedException(Exception e) {
        log.error("不支持当前媒体类型", e);
        Map<String, Object> model = new HashMap<>();
        model.put("errTitle", I18nUtil.message(ExceptionConstants.MEDIA_TYPE));
        model.put("errInfo", e.getMessage());
        return new ModelAndView("/error/500", model);
    }

    /**
     * 500 - 权限异常
     */
    @ExceptionHandler(PermissionException.class)
    public ModelAndView handleException(PermissionException e) {
        log.error("权限异常:", e);
        Map<String, Object> model = new HashMap<>();
        model.put("errTitle", I18nUtil.message(ExceptionConstants.PERMISSION));
        model.put("errInfo", e.getMessage());
        return new ModelAndView("/error/500", model);
    }

    /**
     * 404 - 服务丢失异常
     */
    @ExceptionHandler(NotFoundException.class)
    public ModelAndView handleException(NotFoundException e) {
        log.error("服务丢失异常:", e);
        Map<String, Object> model = new HashMap<>();
        model.put("errTitle", I18nUtil.message(ExceptionConstants.NOT_FOUND));
        model.put("errInfo", "");
        return new ModelAndView("/error/404", model);
    }

    /**
     * 500 - Internal Server Error
     */
    @ExceptionHandler(SerialException.class)
    public ModelAndView handleException(SerialException e) {
        log.error("服务运行异常", e);
        Map<String, Object> model = new HashMap<>();
        model.put("errTitle", I18nUtil.message(ExceptionConstants.SERVER));
        model.put("errInfo", e.getMessage());
        return new ModelAndView("/error/500", model);
    }

    /**
     * 500 - Internal Server Error
     */
    @ExceptionHandler(Exception.class)
    public ModelAndView handleException(Exception e) {
        log.error("未知异常", e);
        Map<String, Object> model = new HashMap<>();
        model.put("errTitle", I18nUtil.message(ExceptionConstants.SERVER));
        model.put("errInfo", e.getMessage());
        return new ModelAndView("/error/500", model);
    }
}
